package com.quanyue.music.system.controller;

import com.quanyue.auto.util.ServiceResult;
import com.quanyue.auto.web.Principal;
import com.quanyue.auto.web.Scope;
import com.quanyue.music.domain.constant.UserRole;
import com.quanyue.music.domain.entity.RechargeRule;
import com.quanyue.music.system.mapper.RechargeRuleMapper;
import com.quanyue.music.system.service.RechargeRuleService;
import com.quanyue.music.system.type.RechargeRuleDetail;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

@Api(tags = "设置充值规则")
@RestController
@RequestMapping("/api")
public class RechargeRuleController {
    @Autowired
    private RechargeRuleService rechargeRuleService;
    @Autowired
    private RechargeRuleMapper rechargeRuleMapper;

    @ApiOperation(value = "增加规则")
    @Scope(role = UserRole.MANAGER)
    @PostMapping(value = "/recharge/rule/add", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<Boolean> addRule(@RequestBody RechargeRule rechargeRule) {
        rechargeRuleService.addRule(rechargeRule);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "修改规则")
    @Scope(role = UserRole.MANAGER)
    @PostMapping(value = "/recharge/rule/update", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<Boolean> updateRule(@RequestBody RechargeRule rechargeRule) {
        rechargeRuleService.updateRule(rechargeRule);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "删除规则")
    @Scope(role = UserRole.MANAGER)
    @DeleteMapping(value = "/recharge/rule/delete", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<Boolean> addRule(@RequestParam("id") String id) {
        rechargeRuleService.deleteRule(id);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "查询规则")
    @GetMapping(value = "/recharge/rule/select", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<RechargeRuleDetail> selectRule(Principal principal) {
        RechargeRuleDetail detail = new RechargeRuleDetail();
        detail.setRechargeRuleList(rechargeRuleService.findRules());
        detail.setShareCount(rechargeRuleMapper.findSharCount(principal.getId()));
        detail.setFlowType(rechargeRuleMapper.findFlowByUserId(principal.getId()));
        return ServiceResult.success(detail);
    }
}
